from flask import Blueprint, render_template, request, redirect, url_for, flash
from flask_login import login_required, current_user
from models.service import Service
from extensions import db
from datetime import datetime

service_bp = Blueprint('service', __name__)

@service_bp.route('/list')
@login_required
def service_list():
    # 获取查询参数
    order_id = request.args.get('order_id', '')
    customer_name = request.args.get('customer_name', '')
    service_type = request.args.get('service_type', '')
    status = request.args.get('status', '')
    
    # 构建查询
    query = Service.query
    if order_id:
        query = query.filter(Service.order_id.like(f'%{order_id}%'))
    if customer_name:
        query = query.filter(Service.customer_name.like(f'%{customer_name}%'))
    if service_type:
        query = query.filter(Service.service_type == service_type)
    if status:
        query = query.filter(Service.status == status)
    
    # 分页
    page = request.args.get('page', 1, type=int)
    pagination = query.order_by(Service.created_at.desc()).paginate(
        page=page, per_page=10, error_out=False)
    
    return render_template('service/service_list.html',
                         services=pagination.items,
                         pagination=pagination,
                         order_id=order_id,
                         customer_name=customer_name,
                         service_type=service_type,
                         status=status)

@service_bp.route('/add', methods=['GET', 'POST'])
@login_required
def service_add():
    if request.method == 'POST':
        service = Service(
            order_id=request.form.get('order_id'),
            customer_name=request.form.get('customer_name'),
            contact_phone=request.form.get('contact_phone'),
            service_type=request.form.get('service_type'),
            description=request.form.get('description'),
            priority=request.form.get('priority'),
            notes=request.form.get('notes')
        )
        db.session.add(service)
        db.session.commit()
        flash('售后服务记录创建成功', 'success')
        return redirect(url_for('service.service_list'))
    return render_template('service/service_add.html')

@service_bp.route('/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def service_edit(id):
    service = Service.query.get_or_404(id)
    if request.method == 'POST':
        service.customer_name = request.form.get('customer_name')
        service.contact_phone = request.form.get('contact_phone')
        service.service_type = request.form.get('service_type')
        service.description = request.form.get('description')
        service.status = request.form.get('status')
        service.priority = request.form.get('priority')
        service.solution = request.form.get('solution')
        service.notes = request.form.get('notes')
        
        if service.status == 'completed' and not service.completed_at:
            service.completed_at = datetime.now()
            service.handler_id = current_user.id
            
        db.session.commit()
        flash('售后服务记录更新成功', 'success')
        return redirect(url_for('service.service_list'))
    return render_template('service/service_edit.html', service=service)

@service_bp.route('/delete/<int:id>')
@login_required
def service_delete(id):
    service = Service.query.get_or_404(id)
    if service.status in ['processing', 'completed']:
        flash('处理中或已完成的服务记录不能删除', 'error')
        return redirect(url_for('service.service_list'))
    
    db.session.delete(service)
    db.session.commit()
    flash('售后服务记录删除成功', 'success')
    return redirect(url_for('service.service_list')) 